package com.java.springboot.controller;

import com.java.springboot.mapper.Bookmapper;
import com.java.springboot.model.User;
import com.java.springboot.service.UserserviceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class logincontroller {

    @Autowired
    private UserserviceImpl userservice;


    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(String username,String password){
        Subject subject = SecurityUtils.getSubject();
        //散列算法给密码加密，用户名作为盐
        String md5Hash = new Md5Hash(password, username).toString();
        UsernamePasswordToken Token = new UsernamePasswordToken(username, md5Hash);
        try{
            subject.login(Token);
            return "main";
        }catch (AuthenticationException e){
            return "index";
        }




    }

    @RequestMapping(value ="index",method = RequestMethod.GET)
    public String index(){
        return "login";
    }
}
